package com.hjwoo.app.views;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;

import com.hjwoo.app.R;
import com.hjwoo.app.utils.LoanToast;

/**
 * Created by zhaokaiwen on 2016/7/10.
 */
public class WritePadActivity extends Activity implements View.OnClickListener {

    private Bitmap cachebBitmap;
    WindowManager.LayoutParams p ;
    boolean firstFlag = true;
    //	上:up; 下:down; 左:left; 右:right.
    public float up,down,left,right;
    //画图视图
    PaintView mView;
    DisplayMetrics dm;
    static final int BACKGROUND_COLOR = Color.WHITE;
    static final int BRUSH_COLOR = Color.BLACK;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

//        /*set it to be no title*/
//        requestWindowFeature(Window.FEATURE_NO_TITLE);
//
//        /*set it to be full screen*/
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        // 隐藏应用程序的标题栏，即当前activity的标题栏
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.write_pad_activity);
        firstFlag =true;

        setTitle("欢迎使用手写签名");
        findViewById(R.id.tablet_ok).setOnClickListener(this);
        findViewById(R.id.tablet_cancel).setOnClickListener(this);
        findViewById(R.id.tablet_clear).setOnClickListener(this);

        dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
//        SCREEN_WIDTH = dm.widthPixels;
//        SCREEN_HEIGHT = dm.heightPixels;

        FrameLayout frameLayout = (FrameLayout) findViewById(R.id.tablet_view);

        p = getWindow().getAttributes();  //获取对话框当前的参数值
        p.height = dm.heightPixels;//(int) (d.getHeight() * 0.4);   //高度设置为屏幕的0.4
        p.width = dm.widthPixels;//(int) (d.getWidth() * 0.6);    //宽度设置为屏幕的0.6

        getWindow().setAttributes(p);     //设置生效
        mView = new PaintView(this);
        frameLayout.addView(mView);
        mView.requestFocus();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tablet_ok:

                try {
                    Bitmap bt = mView.getCachebBitmap();
                    Log.e("ee",(int)left+" "+ (int)up+" "+ (int) (right-left)+" "+ (int) (down-up));
//                    Log.e("ee",(int)up+" "+ (int)down+" "+ (int)left+" "+ (int) right);

                    int width = (int) (right-left);
                    int height = (int) (down-up);
                    if ((int)left !=0 && (int)up !=0 && width !=0 && height!=0){
                        Bitmap bitmap = Bitmap.createBitmap(bt, (int)left, (int)up, width, height);
                        DeclarationSuccessActivity.signbitmap = bitmap;
                        setResult(RESULT_OK);
                        finish();
                    }else{
                        LoanToast.showToastShort("请签名",WritePadActivity.this);
                    }

//                    dialogListener.refreshActivity(bitmap);
//					dialogListener.refreshActivity(mView.getCachebBitmap());
//                    WritePadDialog.this.dismiss();

//                    setResult(RESULT_OK,new Intent().putExtra("writebitmap",bitmap));

                } catch (Exception e) {
                    e.printStackTrace();
                }

                break;
            case R.id.tablet_cancel:
                finish();
                break;
            case R.id.tablet_clear:
                //清除
                mView.clear();
                break;
        }
    }

    /**
     * This view implements the drawing canvas.
     *
     * It handles all of the input events and drawing functions.
     */
    class PaintView extends View {
        private Paint paint;
        private Canvas cacheCanvas;
        //		private Bitmap cachebBitmap;
        private Path path;

        public Bitmap getCachebBitmap() {
            return cachebBitmap;
        }

        public PaintView(Context context) {
            super(context);
            init();
        }

        private void init(){
            paint = new Paint();
            paint.setAntiAlias(true);
            paint.setStrokeWidth(6);
            paint.setStyle(Paint.Style.STROKE);
            paint.setColor(Color.BLACK);
            path = new Path();
            cachebBitmap = Bitmap.createBitmap(p.width, p.height, Bitmap.Config.ARGB_8888);
            cacheCanvas = new Canvas(cachebBitmap);
            cacheCanvas.drawColor(Color.WHITE);


//			cacheCanvas.getHeight();
//			cacheCanvas.getWidth();
        }
        public void clear() {
            if (cacheCanvas != null) {

                paint.setColor(BACKGROUND_COLOR);
                cacheCanvas.drawPaint(paint);
                paint.setColor(Color.BLACK);
                cacheCanvas.drawColor(Color.WHITE);
                invalidate();
                firstFlag = true;
                //将距左边的距离设置成0 触发已签名
                left = 0;
            }
        }



        @Override
        protected void onDraw(Canvas canvas) {
            // canvas.drawColor(BRUSH_COLOR);
            canvas.drawBitmap(cachebBitmap, 0, 0, null);
            canvas.drawPath(path, paint);

        }

        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {

            int curW = cachebBitmap != null ? cachebBitmap.getWidth() : 0;
            int curH = cachebBitmap != null ? cachebBitmap.getHeight() : 0;
            if (curW >= w && curH >= h) {
                return;
            }

            if (curW < w)
                curW = w;
            if (curH < h)
                curH = h;

            Bitmap newBitmap = Bitmap.createBitmap(curW, curH, Bitmap.Config.ARGB_8888);
            Canvas newCanvas = new Canvas();
            newCanvas.setBitmap(newBitmap);
            if (cachebBitmap != null) {
                newCanvas.drawBitmap(cachebBitmap, 0, 0, null);
            }
            cachebBitmap = newBitmap;
            cacheCanvas = newCanvas;
        }

        private float cur_x, cur_y;

        @Override
        public boolean onTouchEvent(MotionEvent event) {

            float x = event.getX();
            float y = event.getY();

            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN: {
                    cur_x = x;
                    cur_y = y;
                    path.moveTo(cur_x, cur_y);
                    break;
                }

                case MotionEvent.ACTION_MOVE: {
                    Log.e("tt","x:"+x+"y:" + y);

                    p.height = dm.heightPixels;//(int) (d.getHeight() * 0.4);   //高度设置为屏幕的0.4
                    p.width = dm.widthPixels;//(int) (d.getWidth() * 0.6);    //宽度设置为屏幕的0.6

                    if (x<=0||y<=0||x>=dm.widthPixels||y >=dm.heightPixels){
                        break;
                    }

                    if (firstFlag){
                        up = y;down = y;
                        left = x;right = x;
                        firstFlag = false;
                    }

                    if (y<up) {up = y;}
                    if (y>down) {down = y;}
                    if (x<left) {left = x;}
                    if (x>right) {right = x;}
                    Log.e("ee",(int)up+" "+ (int)down+" "+ (int)left+" "+ (int) right);

                    path.quadTo(cur_x, cur_y, x, y);
                    cur_x = x;
                    cur_y = y;
                    break;
                }

                case MotionEvent.ACTION_UP: {
                    cacheCanvas.drawPath(path, paint);
                    path.reset();
                    break;
                }
            }
            invalidate();
            return true;
        }
    }
}
